<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{
    protected $primaryKey = 'id';
    protected $guarded = [];
    protected $table = 'admins';


    public function roles()
    {
        return $this->belongsToMany('App\Models\Role', 'admin_role', 'admin_id', 'role_id')->withPivot(
            ['role_id', 'admin_id']
        );
    }

    public function roleIds(){
        return $this->hasMany('App\Models\AdminRole','admin_id','id');
    }
    public function hasPermission($permission){
        return !!$this->roles->intersect($permission->roles)->count();
    }

    public function permissions(){
        $role_ids = $this->roleIds()->pluck('role_id')->toArray();

        $permission_ids = array_unique(RolePermission::whereIn('role_id',$role_ids)->pluck('permission_id')->toArray());

        $permissions = Permission::whereIn('id',$permission_ids)->get()->pluck('name')->toArray();

        return $permissions;
    }
}
